<template>
    <table class="table table-bordered" border="1">
        <tr>
            <td>机构名称</td>
            <td><input type="text" v-model="info.jgName" /></td>
        </tr>
        <tr>
            <td>机构图片</td>
            <td>
                <input type="file" @change="load" />
                <img :src="info.img" style="max-width: 120px; max-height: 120px" />
            </td>
        </tr>
        <tr>
            <td>服务特色</td>
            <td>
                <select v-model="info.tsfw">
                    <option>请选择</option>
                    <option v-for="item in ints" :value="item.jgts">{{ item.jgts }}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>营业时间</td>
            <td><input type="text" v-model="info.yysj" /></td>
        </tr>
        <tr>
            <td>机构地址</td>
            <td><input type="text" v-model="info.addr" /></td>
        </tr>
        <tr>
            <td>联系电话</td>
            <td><input type="text" v-model="info.jgPhone" /></td>
        </tr>
        <tr>
            <td>发布时间</td>
            <td><input type="date" v-model="info.fbtime" /></td>
        </tr>
        <tr>
            <td>机构详情</td>
            <td><textarea name="" id="" cols="30" rows="10" v-model="info.jgxc"></textarea></td>
        </tr>
        <tr>
            <td class="2"></td>
            <td><input type="button" value="添加" @click="add" /></td>
        </tr>
    </table>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import axios from 'axios'
const info = ref({ jgId: 0, jgName: '', img: '', tsfw: '', yysj: '', addr: '', jgPhone: '', fbtime: '', jgxc: '', fx: '100', sc: '50', stary: true, gxr: '小王', isdel: true })
const load = (e: any) => {
    let file = e.target.files[0]
    const formData = new FormData()
    formData.append('file', file)
    axios.post('https://localhost:7190/api/User/load', formData).then(res => {
        info.value.img = res.data
    })
}

onMounted(() => {
    ts()
})
const add = () => {
    if (info.value.jgName == '') {
        alert('机构名称不能为空')
        return
    }
    if (info.value.addr == '') {
        alert('地址不能为空')
        return
    }
    if (info.value.fbtime == '') {
        alert('发布时间不能为空')
        return
    }
    if (info.value.img == '') {
        alert('图片不能为空')
        return
    }

    axios.post('https://localhost:7190/api/User/JgAdd', info.value).then(res => {
        if (res.data > 0) {
            console.log(res.data)

            alert('添加成功')
            location.href = 'show'
        } else {
            alert('添加失败')
        }
    })
}
const ints = ref([
    {
        tsid: 0,
        jgts: ''
    }
])
const ts = () => {
    axios.get('https://localhost:7190/api/User/tsshow').then(res => {
        ints.value = res.data
    })
}
</script>
